public class IsPalindrome {

    public static void main(String[] args) {
        IsPalindrome isPalindrome = new IsPalindrome();
        System.out.println(isPalindrome.isPalindrome("race a car"));
    }

    public boolean isPalindrome(String s) {
        String filter = filter(s);
        if (filter.length() == 0) return true;
        int l = 0;
        int r = filter.length() - 1;
        while (l <= r) {
            if (filter.charAt(l) != filter.charAt(r)) {
                return false;
            }
            l++;
            r--;
        }
        return true;
    }

    public String filter(String s) {
        StringBuffer stringBuffer = new StringBuffer();
        String s1 = s.toUpperCase();
        for (int i = 0; i < s1.length(); i++) {
            char c = s1.charAt(i);
            if ((c >= 65 && c <= 90) || (c >= 48 && c <= 57)) {
                stringBuffer.append(c + "");
            }
        }
        return stringBuffer.toString();
    }
}
